package com.irdstudio.efp.esb.api.rest.yx;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.List;
import java.util.Objects;

import javax.servlet.http.HttpServletRequest;

import com.irdstudio.efp.console.service.facade.PrdInfoService;
import com.irdstudio.efp.console.service.vo.PrdInfoVO;
import com.irdstudio.efp.esb.api.common.AmtUtil;
import com.irdstudio.efp.esb.api.common.GroupeInterface;
import com.irdstudio.efp.esb.common.constant.InstitutionEnums;
import com.irdstudio.efp.loan.common.LoanConstant;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.irdstudio.basic.framework.core.base.FrameworkService;
import com.irdstudio.basic.framework.core.constant.BaseConstant;
import com.irdstudio.basic.framework.core.constant.CurrencyConstant;
import com.irdstudio.basic.framework.core.exception.BizException;
import com.irdstudio.basic.framework.core.exception.ESBException;
import com.irdstudio.basic.framework.core.util.DateTool;
import com.irdstudio.basic.framework.core.util.DateUtility;
import com.irdstudio.basic.framework.core.util.MessageRuleCheckUtil;
import com.irdstudio.basic.framework.core.util.StringUtil;
import com.irdstudio.efp.console.common.ConsoleConstant;
import com.irdstudio.efp.ctr.common.ContConstant;
import com.irdstudio.efp.ctr.service.facade.CtrLoanContService;
import com.irdstudio.efp.ctr.service.vo.CtrLoanContVO;
import com.irdstudio.efp.esb.api.bean.yx.ReqBraLoanBean;
import com.irdstudio.efp.esb.api.bean.yx.RespBraLoanBean;
import com.irdstudio.efp.esb.api.common.EsbRespServiceBeanCreator;
import com.irdstudio.efp.esb.api.common.SDicServerConstant;
import com.irdstudio.efp.esb.api.util.SDicMappingServer;
import com.irdstudio.efp.esb.common.constant.EsbBizEnums;
import com.irdstudio.efp.esb.common.constant.EsbSysHeadEnums;
import com.irdstudio.efp.esb.common.constant.bd.EventTypeEnum;
import com.irdstudio.efp.esb.common.constant.nls.NlsProdCdEnum;
import com.irdstudio.efp.esb.common.server.req.EsbReqServiceBean;
import com.irdstudio.efp.esb.common.server.resp.EsbRespServiceBean;
import com.irdstudio.efp.limit.service.facade.LmtPrdContService;
import com.irdstudio.efp.limit.service.vo.LmtPrdContVO;
import com.irdstudio.efp.loan.service.facade.AccLoanService;
import com.irdstudio.efp.loan.service.vo.AccLoanVO;
import com.irdstudio.efp.nls.service.facade.NlsApplyInfoService;
import com.irdstudio.efp.nls.service.facade.BdLoanpayService;
import com.irdstudio.efp.nls.service.facade.ed.LmtRecoverRecordService;
import com.irdstudio.efp.nls.service.vo.NlsApplyInfoVO;
import com.irdstudio.efp.nls.service.vo.BdLoanpayVO;
import com.irdstudio.efp.nls.service.vo.ed.LmtRecoverRecordVO;

@RestController
@RequestMapping("/esb")
public class BraLoanController implements FrameworkService {
	// 单位转换
	private final int oneHundred_100 = 100;
	private static Logger logger = LoggerFactory.getLogger(BraLoanController.class);

	@Autowired
	@Qualifier("nlsApplyInfoService")
	private NlsApplyInfoService nlsApplyInfoService;
	@Autowired
	@Qualifier("bdLoanpayService")
	private BdLoanpayService bdLoanpayService;
	@Autowired
	@Qualifier("accLoanService")
	private AccLoanService accLoanService;
	@Autowired
	@Qualifier("lmtPrdContService")
	private LmtPrdContService lmtPrdContService;
	@Autowired
	@Qualifier("ctrLoanContService")
	private CtrLoanContService ctrLoanContService;
	@Autowired
	@Qualifier("lmtRecoverRecordService")
	private LmtRecoverRecordService  lmtRecoverRecordService;
	@Autowired
	@Qualifier("prdInfoService")
	private PrdInfoService prdInfoService;

	
	/**
	 * 获取ESB请求对象，校验 ESB请求系统头和请求应用头对象
	 * 
	 * @param request
	 *            请求对象集合
	 * @return
	 * @author huangwb
	 * @since 2019年1月20日 下午1:28:08
	 * @version 1.0
	 */
	@ModelAttribute("esbReqServiceBean")
	public EsbReqServiceBean getReqEsbHeadBean(HttpServletRequest request) {
		logger.info("获取ESB请求对象，校验 ESB请求系统头和请求应用头对象开始");
		EsbReqServiceBean esbReqServiceBean = (EsbReqServiceBean) request.getAttribute("esbReqServiceBean");
		return esbReqServiceBean;
	}

	/**
	 * 获取ESB请求对象，校验 ESB请求体对象
	 * 
	 * @param request
	 *            请求对象集合
	 * @return
	 * @author liyakui
	 * @since 2019年1月20日 下午1:31:14
	 * @version 1.0
	 */
	@ModelAttribute("reqBraLoanBean")
	public ReqBraLoanBean getReqEsbBodyBean(HttpServletRequest request) {
		logger.info("获取放款结果通知请求对象，校验放款结果通知请求体对象开始");
		String esbReqBody = (String) request.getAttribute("esbReqBody");
		return JSONObject.parseObject(esbReqBody, ReqBraLoanBean.class);
	}
	
	/**
	 * 用信贷款通知
	  * @author heyingfeng
	  * @since 2019年3月13日 下午4:41:27
	  * @version 1.0
	 */
	@PostMapping("/SERVICE_30220007_04")
	public EsbRespServiceBean doRecv(@Validated @ModelAttribute("esbReqServiceBean") EsbReqServiceBean esbReqServiceBean, BindingResult br1,
									 @Validated({GroupeInterface.bd.class}) @ModelAttribute("reqBraLoanBean") ReqBraLoanBean reqBraLoanBean, BindingResult br2, Exception exception) {
		try {
			// 1.通过切面校验进件申请参数是否正确，如不正确则返回错误信息
			if (Objects.nonNull(exception)) {
				throw exception;
			}
			// 2.获取交易流水号或订单号
			logger.info("交易流水号[" + reqBraLoanBean.getBsnOrdrNo() + "]放款结果通知开始!");
			// MessageRuleCheckUtil.checkDouble(reqBraLoanBean.getLoanAmt(), "贷款金额");
			MessageRuleCheckUtil.checkDouble(reqBraLoanBean.getOrgAmt(), "机构金额");
			
			// 交易时间转换
			String tranTime = DateTool.formateDate(DateUtility.formatDateTime17To19(reqBraLoanBean.getTxnTmstmp()));

			// 3.根据交易流水号查询网贷申请基本表
			NlsApplyInfoVO nlsApplyInfoVo = new NlsApplyInfoVO();
			nlsApplyInfoVo.setBdOrderId(reqBraLoanBean.getBsnOrdrNo());
			nlsApplyInfoVo.setCertCode(reqBraLoanBean.getCertNo());

			nlsApplyInfoVo = nlsApplyInfoService.queryByPk(nlsApplyInfoVo);
			if (nlsApplyInfoVo == null || "".equals(nlsApplyInfoVo.getApplySeq())) {
				throw new BizException("无用信数据");
			}
//			String prdCode = nlsApplyInfoVo.getPrdCode();//产品代码
			
			// 额度
			LmtPrdContVO lmtPrdContVo = new LmtPrdContVO();
			lmtPrdContVo.setLmtApplySeq(nlsApplyInfoVo.getLmtApplySeq());
//			if(NlsProdCdEnum.MYD.getValue().equals(nlsApplyInfoVo.getPrdCode())){//满易贷   查询额度状态
//				lmtPrdContVo.setLmtStatus(ConsoleConstant.LIMIT_STS_02);
//			}
			lmtPrdContVo = lmtPrdContService.queryByPk(lmtPrdContVo);
			if (lmtPrdContVo == null) {
				throw new BizException("无额度数据");
			}
//			BigDecimal applyAmt = nlsApplyInfoVo.getApplyAmt();//申请金额
//			BigDecimal preOccAmt = lmtPrdContVo.getPreOccAmt();//上次预占用
//			BigDecimal occAmt = lmtPrdContVo.getOccAmt();//上次已用
//			BigDecimal lmtAmt = lmtPrdContVo.getLmtAmt();//授信额度
			//
			//
			BigDecimal loanAmt = nlsApplyInfoVo.getApplyAmt(); //贷款金额
//			if(loanAmt.compareTo(lmtAmt)==1) {
//				throw new BizException("归还金额本金大于授信额度");
//			}
			// 根据产品代码查询产品id
			PrdInfoVO vo = new PrdInfoVO();
			vo.setPrdCode(nlsApplyInfoVo.getPrdCode());
			PrdInfoVO outputVo = prdInfoService.queryLastPrdInfo(vo);
			if (outputVo == null || "".equals(outputVo.getPrdId())) {
				throw new BizException("无产品id");
			}
			BigDecimal maxAmt = outputVo.getLoanAppMaxAmt();
			BigDecimal minAmt = outputVo.getLoanAppMinAmt();
			// 产品最大授信额度判断
			if(loanAmt.compareTo(maxAmt) == 1) {
				throw new BizException("用信申请金额：" + "【" +loanAmt + "】" + "大于产品最大支用额度：" + "【" + maxAmt + "】");
			}
			// 产品最小授信额度判断
			if(loanAmt.compareTo(minAmt) == -1) {
				throw new BizException("用信申请金额：" + "【" +loanAmt + "】" + "小于产品最小支用额度：" + "【" + minAmt + "】");
			}
			//合同
			CtrLoanContVO ctrLoanContVo = new CtrLoanContVO();
			ctrLoanContVo.setContNo(nlsApplyInfoVo.getApplySeq());
			//借据
			AccLoanVO accLoanVo = new AccLoanVO();
			accLoanVo.setBillNo(reqBraLoanBean.getDblNo());
			accLoanVo = accLoanService.queryByPk(accLoanVo);
			if (accLoanVo != null) {
				throw new BizException("重复借据号");
			}
			
			//额度恢复
			LmtRecoverRecordVO LmtRecoverRecordVo = new LmtRecoverRecordVO();
			// 判断是否正常审批通过
			if (!BaseConstant.NLS_APPLY_STATE_02.equals(nlsApplyInfoVo.getNlsApplyState())) {
				logger.debug("网贷审批不通过,不应放款！");
				return EsbRespServiceBeanCreator.buildesbRespServiceBean(esbReqServiceBean,
						JSON.toJSONString(buildRespNoticeOfResultBean(EsbBizEnums.RetCodeEnum.YWLJCLYC.getVALUE(), "网贷审批不通过,不应放款！",
								reqBraLoanBean.getAppFlowNo())),
						EsbSysHeadEnums.RetStEnum.SUCCESS.getCode(), EsbBizEnums.RetCodeEnum.YWLJCLYC.getVALUE(), "网贷审批不通过,不应放款！");
			}
			// 判断是否重复发起放款结果通知
			String dnSts = nlsApplyInfoVo.getDnSts();

			if (BaseConstant.DN_STS_0.equals(dnSts) || BaseConstant.DN_STS_1.equals(dnSts)) {
				// 已经放款失败，勿重发
				logger.debug("放款已处理，勿重发！");
				return EsbRespServiceBeanCreator.buildesbRespServiceBean(esbReqServiceBean,
						JSON.toJSONString(buildRespNoticeOfResultBean(EsbBizEnums.RetCodeEnum.YWLJCLYC.getVALUE(), "放款已处理，勿重发！",
								reqBraLoanBean.getAppFlowNo())),
						EsbSysHeadEnums.RetStEnum.SUCCESS.getCode(), EsbBizEnums.RetCodeEnum.YWLJCLYC.getVALUE(), "放款已处理，勿重发！");
			} else {
				//插入放款申请表
				BdLoanpayVO bdLoanpayVO = new BdLoanpayVO();
				bdLoanpayVO.setApplySeq(reqBraLoanBean.getAppFlowNo()); //申请流水号
				bdLoanpayVO.setEventType(reqBraLoanBean.getEvID()); //事件码
				bdLoanpayVO.setVersion (reqBraLoanBean.getVerNo()); //接口版本号
				bdLoanpayVO.setComment (reqBraLoanBean.getRmk()); //备注
				bdLoanpayVO.setOrderId(reqBraLoanBean.getBsnOrdrNo()); //业务订单号
				bdLoanpayVO.setCurDate(DateUtility.format8To10(reqBraLoanBean.getAcctDt())); //账务日期
				bdLoanpayVO.setTranTime(tranTime); //交易时间 
				bdLoanpayVO.setLeader(reqBraLoanBean.getLeaderCd()); //牵头方
				bdLoanpayVO.setPartner(reqBraLoanBean.getPrtnrCd()); //合作方
				bdLoanpayVO.setCusName(reqBraLoanBean.getCustNm()); //客户姓名
				bdLoanpayVO.setCertType(reqBraLoanBean.getCertTp()); //证件类型
				bdLoanpayVO.setCertNo(reqBraLoanBean.getCertNo()); //证件号
				bdLoanpayVO.setLoanId(reqBraLoanBean.getDblNo()); //借据号
				bdLoanpayVO.setApplyDate(DateUtility.format8To10(reqBraLoanBean.getAplDt())); //申请日期
				bdLoanpayVO.setStartDate(DateUtility.format8To10(reqBraLoanBean.getCfrmDt())); //确认日期
				bdLoanpayVO.setFirstDueDate(DateUtility.format8To10(reqBraLoanBean.getFrstRpyDt())); //第一期还款日
				bdLoanpayVO.setDueDay(reqBraLoanBean.getRpyDt()); //还款日
				bdLoanpayVO.setEncashAmt(StringUtil.isNullorBank(reqBraLoanBean.getOrgAmt()) ? BigDecimal.ZERO : new BigDecimal(reqBraLoanBean.getOrgAmt())); //机构金额
				bdLoanpayVO.setLoanCcy(CurrencyConstant.CNY); //币种
				bdLoanpayVO.setRepayMode(StringUtil.isNullorBank(reqBraLoanBean.getRpyMd()) ? "" : SDicMappingServer.getLocalSysDicVal(SDicServerConstant.REPAY_WAY, reqBraLoanBean.getRpyMd())); //还款方式
				bdLoanpayVO.setRepayCycle(reqBraLoanBean.getRpyCyc()); //还款周期
				bdLoanpayVO.setTotalTerms(reqBraLoanBean.getTotRpyTerm()); //总期数
				bdLoanpayVO.setGraceDay(reqBraLoanBean.getGrcTerm()); //宽限期 
				bdLoanpayVO.setScheduleNo (reqBraLoanBean.getAmortTerm()); //期数
				bdLoanpayVO.setContStartDate(DateUtility.format8To10(reqBraLoanBean.getStrtDt())); //开始日 
				bdLoanpayVO.setContEndDate(DateUtility.format8To10(reqBraLoanBean.getMateDt())); //到期日
				bdLoanpayVO.setAmounts(loanAmt); //金额
				bdLoanpayVO.setCreateUser("admin");//创建人
				bdLoanpayVO.setCreateTime(DateUtility.getCurrAppDateTimeString());//创建时间 
				bdLoanpayVO.setDnSts(BaseConstant.DN_STS_3); //更新放款状态为待放款
				int num1 = bdLoanpayService.insertBdLoanpay(bdLoanpayVO);
				if (num1 == -1) {
					throw new BizException("插入放款通知表异常!");
				}
				if (EventTypeEnum.BDFKCG.getValue().equals(reqBraLoanBean.getEvID())) {//百度放款成功
					nlsApplyInfoVo.setDnSts(BaseConstant.DN_STS_1);// 更新状态
					nlsApplyInfoVo.setAmountOfLoan(loanAmt.toString());// 放款金额
					nlsApplyInfoVo.setLastModifyTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));// 修改更新时间
					bdLoanpayVO.setDnSts(BaseConstant.DN_STS_1); //更新放款状态为放款成功
					//额度更新
					//上次预占用-本次支用金额
//					lmtPrdContVo.setPreOccAmt(preOccAmt.subtract(loanAmt));
					//上次已用
//					lmtPrdContVo.setOccAmt(occAmt.add(loanAmt));
					//可用金额=授信金额-占用金额-已用金额
//					lmtPrdContVo.setAvailAmt(lmtPrdContVo.getLmtAmt().subtract(lmtPrdContVo.getPreOccAmt()).subtract(lmtPrdContVo.getOccAmt()));
					if(NlsProdCdEnum.ZXD.getValue().equals(nlsApplyInfoVo.getPrdId())){//尊享贷设置状态失效
						lmtPrdContVo.setLmtStatus(ConsoleConstant.LIMIT_STS_03);
						lmtPrdContVo.setAvailAmt(BigDecimal.ZERO);
					}
//					if(lmtPrdContVo.getAvailAmt().compareTo(lmtAmt) == 1) {
//						throw new BizException("可用额度大于授信额度");
//					}
					//更新进件表
					int a = nlsApplyInfoService.updateByPk(nlsApplyInfoVo);
					if (a == -1) {
						throw new BizException("更新用信数据表异常!");
					}
					//更新额度表
					int b = lmtPrdContService.updateByPk(lmtPrdContVo);
					if (b == -1) {
						throw new BizException("更新额度数据表异常!");
					}
					//更新放款申请表
					int c = bdLoanpayService.updateByOrderId(bdLoanpayVO);
					if (c == -1) {
						throw new BizException("更新放款数据表异常!");
					}
					// 4.若放款成功向拮据表插入一条借据信息
					accLoanVo = new AccLoanVO();
					accLoanVo.setBillNo(reqBraLoanBean.getDblNo());
					accLoanVo.setCusId(nlsApplyInfoVo.getCusId());
					accLoanVo.setCusName(nlsApplyInfoVo.getCusName());
					accLoanVo.setPrdId(nlsApplyInfoVo.getPrdId());
					accLoanVo.setPrdCode(nlsApplyInfoVo.getPrdCode());
					accLoanVo.setPrdName(outputVo.getPrdName());
					accLoanVo.setContNo(nlsApplyInfoVo.getApplySeq());// 合同号
					accLoanVo.setLoanAmount(loanAmt);// 借据金额
					accLoanVo.setLoanBalance(loanAmt);// 借据余额
					accLoanVo.setLoanStartDate(nlsApplyInfoVo.getApplyDate());
					accLoanVo.setLoanEndDate(LocalDate.parse(nlsApplyInfoVo.getApplyDate())
							.plusMonths(Long.valueOf(nlsApplyInfoVo.getBdTerm()))
							.format(DateTimeFormatter.ISO_LOCAL_DATE));// 贷款到期日
					accLoanVo.setLoanTerm(new BigDecimal(nlsApplyInfoVo.getLoanTerm() == null ? "0" : nlsApplyInfoVo.getLoanTerm()));// 贷款期限
					accLoanVo.setLoadTermType(nlsApplyInfoVo.getLoanTermType());// 期限类型
					accLoanVo.setEnterAccount(nlsApplyInfoVo.getRecvAcctNo());// 放款账号
					// modify by ludayong 2019/11/21 【2019D1127】更新借据状态为出帐未确认
					accLoanVo.setAccountStatus(LoanConstant.TZZT_WQR);
					accLoanVo.setCreateTime(new SimpleDateFormat("yyyy-MM-dd").format(new Date()));
					accLoanVo.setCurType(nlsApplyInfoVo.getLoanCcy());// 币种
					accLoanVo.setRepaymentMode(nlsApplyInfoVo.getRepayMethod());// 还款方式
					accLoanVo.setCertCode(nlsApplyInfoVo.getCertCode());
					accLoanVo.setCertType(nlsApplyInfoVo.getCertType());
					accLoanVo.setRealityIrY(new BigDecimal(nlsApplyInfoVo.getBdCompreAnnualInterestRate()));
					accLoanVo.setChannelCode(nlsApplyInfoVo.getChannelNo()); // 渠道编号
					accLoanVo.setApplySeq(nlsApplyInfoVo.getApplySeq());// 用信流水号
					// add by ludayong 2019/11/15 【数据权限】增加登记机构赋值
					// 登记机构
					accLoanVo.setInputBrId(InstitutionEnums.InstitutionEnum.GZYHZHYHZXSCTD1.getCode());
					int num2 = accLoanService.insertAccLoan(accLoanVo);
					if (num2 == -1) {
						throw new BizException("插入借据表异常!");
					}
					logger.debug("放款成功！");
					return EsbRespServiceBeanCreator.buildesbRespServiceBean(esbReqServiceBean,
							JSON.toJSONString(buildRespNoticeOfResultBean(EsbBizEnums.RetCodeEnum.SUCCESS.getVALUE(), "放款成功！",
									reqBraLoanBean.getAppFlowNo())),
							EsbSysHeadEnums.RetStEnum.SUCCESS.getCode(), EsbBizEnums.RetCodeEnum.SUCCESS.getVALUE(), "放款成功！");
				} else {
					logger.debug("放款失败！");
					if(BaseConstant.DN_STS_3.equals(dnSts)) {//待放款----放款失败
						//判断网贷系统放款状态是否为失败
						ctrLoanContVo.setContState(ContConstant.CONT_STATUS_201);//更新合同状态"失效"
						nlsApplyInfoVo.setDnSts(BaseConstant.DN_STS_0);// 更新放款状态为失效
						nlsApplyInfoVo.setLastModifyTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));// 修改更新时间
						bdLoanpayVO.setDnSts(BaseConstant.DN_STS_0); //更新放款通知表放款结果为失败
						
						//上次预占用-本次支用金额
//						lmtPrdContVo.setPreOccAmt(preOccAmt.subtract(loanAmt));
						//上次已用
//						lmtPrdContVo.setOccAmt(occAmt);
						//可用金额=授信金额-占用金额-已用金额
//						if(NlsProdCdEnum.MYD.getValue().equals(nlsApplyInfoVo.getPrdCode())){//满易贷更新可用额度
//							lmtPrdContVo.setAvailAmt(lmtAmt.subtract(lmtPrdContVo.getPreOccAmt()).subtract(lmtPrdContVo.getOccAmt()));
//						}
//						if(lmtPrdContVo.getAvailAmt().compareTo(lmtAmt) == 1) {
//							throw new BizException("可用额度大于授信额度");
//						}
						//生成额度恢复记录
						LmtRecoverRecordVo.setLmtRecoverSeq(nlsApplyInfoVo.getBdOrderId());//额度恢复流水号
						LmtRecoverRecordVo.setLmtApplySeq(nlsApplyInfoVo.getApplySeq());//授信申请流水号
						LmtRecoverRecordVo.setAccDate(reqBraLoanBean.getAcctDt());//账务日期
						LmtRecoverRecordVo.setRecoverType("3");//额度恢复类型   放款失败
						LmtRecoverRecordVo.setTranTime(reqBraLoanBean.getTxnTmstmp());//交易时间
						LmtRecoverRecordVo.setRecoverAmt(loanAmt+"");//恢复金额
						LmtRecoverRecordVo.setChannelNo(nlsApplyInfoVo.getChannelNo());//渠道号
//						LmtRecoverRecordVo.setRecoverBefAmt(lmtPrdContVo.getLmtAmt()+"");//恢复前额度
//						LmtRecoverRecordVo.setRecoverLatAmt(lmtPrdContVo.getLmtAmt()+"");//恢复后额度
						LmtRecoverRecordVo.setCreateUser("admin");//创建人
						LmtRecoverRecordVo.setCreateTime(DateUtility.getCurrAppDateTimeString());//创建时间
						LmtRecoverRecordVo.setScene(BaseConstant.SCENE_0);//恢复场景(0：实时  1：批量)
						//额度恢复记录表
						//int num2 = lmtRecoverRecordService.insertLmtRecoverRecord(LmtRecoverRecordVo);
						//if (num2 == -1) {
							//throw new BizException("插入额度恢复表异常!");
						//}
						//更新进件表
						int a = nlsApplyInfoService.updateByPk(nlsApplyInfoVo);
						if (a == -1) {
							throw new BizException("更新用信数据表异常!");
						}
						//更新额度表
//						int b = lmtPrdContService.updateByPk(lmtPrdContVo);
//						if (b == -1) {
//							throw new BizException("更新额度数据表异常!");
//						}
						//更新合同表
						int c = ctrLoanContService.updateByPk(ctrLoanContVo);
						if (c == -1) {
							throw new BizException("更新合同数据表异常!");
						}
						//更新放款申请表
						int d = bdLoanpayService.updateByOrderId(bdLoanpayVO);
						if (d == -1) {
							throw new BizException("更新放款数据表异常!");
						}
					}
					return EsbRespServiceBeanCreator.buildesbRespServiceBean(esbReqServiceBean,
							JSON.toJSONString(buildRespNoticeOfResultBean(EsbBizEnums.RetCodeEnum.SUCCESS.getVALUE(), "放款失败！",
									reqBraLoanBean.getAppFlowNo())),
							EsbSysHeadEnums.RetStEnum.SUCCESS.getCode(), EsbBizEnums.RetCodeEnum.SUCCESS.getVALUE(), "放款失败！");
				}
			}
		} catch (ESBException e) {
			logger.error("***********放款结果通知接口调用失败**************"+e.getMessage());
			e.printStackTrace();
			return EsbRespServiceBeanCreator.buildesbRespServiceBean(esbReqServiceBean,
					JSON.toJSONString(buildRespNoticeOfResultBean(EsbBizEnums.RetCodeEnum.OTHERS_EXCEPTION.getVALUE(), e.getMessage(),
							reqBraLoanBean.getAppFlowNo())),
					EsbSysHeadEnums.RetStEnum.FAILURE.getCode(), EsbBizEnums.RetCodeEnum.OTHERS_EXCEPTION.getVALUE(), e.getMessage());
		} catch (Exception e) {
			logger.error("***********放款结果通知接口调用失败**************"+e.getMessage());
			e.printStackTrace();
			return EsbRespServiceBeanCreator.buildesbRespServiceBean(esbReqServiceBean,
					JSON.toJSONString(buildRespNoticeOfResultBean(EsbBizEnums.RetCodeEnum.OTHERS_EXCEPTION.getVALUE(), e.getMessage(),
							reqBraLoanBean.getAppFlowNo())),
					EsbSysHeadEnums.RetStEnum.FAILURE.getCode(), EsbBizEnums.RetCodeEnum.OTHERS_EXCEPTION.getVALUE(), e.getMessage());
		}
	}

	/**
	 * 组织放款结果通知请求接口的返回对象
	 * 
	 * @param retStCd
	 *            返回状态码
	 * @param rtnMsg
	 *            返回信息
	 * @return
	 * @author liyakui
	 * @since 2019年1月20日 下午1:59:46
	 * @version 1.0
	 */
	private static RespBraLoanBean buildRespNoticeOfResultBean(String retStCd, String rtnMsg, String txnFlowNo) {
		RespBraLoanBean respBraLoanBean = new RespBraLoanBean();
		respBraLoanBean.setRetStCd(retStCd);
		respBraLoanBean.setRtnMsg(rtnMsg);
		return respBraLoanBean;
	}

	public static void main(String[] args) {
		System.out.println(	DateUtility.format8To10("2019-07-02"));
	}





	public static String formatDate(Date date, String pattern) {
		return new SimpleDateFormat(pattern).format(date);
	}
}
